<?php

/*
*	$version: 2009-5-19 14:52:25 beta 1.0
*	$id: basic.php
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

if (!defined('IN_LEKO')) {header("HTTP/1.0 403 Forbidden");exit;}

function Cookie($name, $value = "", $permanent = 1, $cookie_setting =	array(	'COOKIEPATH'	=>	'/',
																				'COOKIEPRE'		=>	'', 
																				'COOKIEDOMAIN'	=>	'',
																				'COOKIETIMEOUT'	=>	3600,
																				'SERVER_PORT'	=>	''))
{
	if ($permanent == 1)
		if($cookie_setting['COOKIETIMEOUT'] != 0) $expire = time() + $cookie_setting['COOKIETIMEOUT']; else $expire = time() + 60*60*24*7;
	else
		$expire = time();

	if ($cookie_setting['SERVER_PORT'] == "443") $secure = 1; else $secure = 0;

	if ($cookie_setting['COOKIEPRE'] != '') $name = $cookie_setting['COOKIEPRE'] . $name;

	@setcookie($name, $value, $expire, $cookie_setting['COOKIEPATH'], $cookie_setting['COOKIEDOMAIN'], $secure);
}

function curStr($str, $start, $lenth, $define = '...') {
	if (function_exists('mb_substr'))
	{
		if (mb_strlen($str) <= $lenth)
		{
			return $str;
		}
		else
		{
			$str = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $str);
			$str = mb_substr($str,$start,$lenth);
			$str .= $define;
			$str = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $str);
			return $str;
		}
	}
} // End subString_UTF8

function fileExt($_f) {
	return trim(substr(strrchr($_f, '.'), 1));
}

function hashDir($_id, $_uploadFolder = '', $forceCreate = false) {
	if (!$_id) { return false; }
	$crypt = md5($_id);
	$hashDir = $_uploadFolder . '/' . substr($crypt, 0, 1) . DIRECTORY_SEPARATOR . substr($crypt, 1, 1) . DIRECTORY_SEPARATOR;

	if ($forceCreate !== false)
	{
		if (!is_dir ($_uploadFolder . '/' . substr($crypt, 0, 1)))
			mkdir ($_uploadFolder . '/' . substr($crypt, 0, 1));
		if (!is_dir ($hashDir))
			mkdir ($hashDir);
	}

	return $hashDir;
}

function hash32($_id) {
	return  "_".substr(md5(trim($_id)),0,1);
}

function getReferer() {
	global $_G;

	if(!empty($_SERVER['HTTP_REFERER']))
		$refererUrl = (substr($_SERVER['HTTP_REFERER'], -1) == '?') ? substr($_SERVER['HTTP_REFERER'], 0, -1) : $_SERVER['HTTP_REFERER'];
	else
		$refererUrl = leko_htmlspecialchars("http://www".$_G['domainName']);

	return rawurlencode($refererUrl);
}

function msgBox ($_msg = '' , $_url = '' , $_urlname = '', $_showtime = 3) {
	global $_T,$_G,$_D,$_R;
	if (!$_url) $_url = rawurldecode(getReferer());
	if (!$_url) $_url = "http://www".$_G['domainName'];
	include ($_T->setView("msgBox"));
	exit;
}

function randomString($_length, $numeric = 0) {
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	$string = '';
	if($numeric) {
		$string = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	}
	else {
		$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz_";
		$len = strlen($chars) - 1;
		for($i = 0; $i < $_length; $i++)
			$string .= $chars[mt_rand(0, $len)];
	}
	return $string;
}

?>
